﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using FeatureModelCore;
//using FeatureModelCore.FMShapes;
//using System.Windows.Controls;
//using System.Windows;
//using System.Windows.Media;
//using System.Windows.Input;

//namespace FeatureModelTool.FeatureModelGraphical.GraphicalControls
//{
//    public class Control_TreeNode : IControl
//    {
//        public FeatureTreeNode FeatureInstance { get { return _node; } }
//        public FeatureModelGraphicalView Canvas { get { return _canvas; } }
//        public bool IsFocus { get { return _isFocus; } }
//        public Brush Background { get { if (IsFocus) return Control_TreeRoot.FOCUS_BG_COLOR; return Control_TreeRoot.COMMON_BG_COLOR; } }
//        public Brush StrokeColor { get { if (this._node.ExistError) return Control_TreeRoot.DEAD_STROKE_COLOR; return Control_TreeRoot.COMMON_STROKE_COLOR; } }
//        public Brush ForeColor
//        {
//            get
//            {
//                if (_isFocus)
//                    return Control_TreeRoot.FOCUS_FORE_COLOR;
//                return Control_TreeRoot.COMMON_FORE_COLOR;
//            }
//        }

//        public int BorderThickness
//        {
//            get
//            {
//                if (_isFocus)
//                    return FeatureModelGraphicalView.FOCUS_THICKNESS;
//                return FeatureModelGraphicalView.DEFAULT_THICKNESS;
//            }
//        }
//        #region Fields
//        private FeatureModelGraphicalView _canvas;
//        private FeatureTreeNode _node;
//        private bool _isFocus;

//        private bool _isPressed;
//        private FMPoint _orgPlace;

//        #endregion

//        #region Graphical
//        private Label _textbox;
//        private Control_Relationship _line;
//        #endregion

//        public Control_TreeNode(FeatureModelGraphicalView canvas, FeatureTreeNode node)
//        {
//            _canvas = canvas;
//            _node = node;
//            _isFocus = false;

//            _isPressed = false;
//            _orgPlace = new FMPoint(0, 0);

//            // graphical
//            _textbox = new Label();
//            _textbox.Content = FeatureInstance.FeatureName;

//            // set layout
//            setLayout();

//            _line = new Control_Relationship(canvas, (FeatureTreeGroupRelationship)node.Parent, node);
//            #region Set Mouse Event Handler
//            _textbox.MouseMove += new MouseEventHandler(MouseMove);
//            _textbox.MouseLeftButtonDown += new MouseButtonEventHandler(MouseLeftClickDown);
//            _textbox.MouseLeftButtonUp += new MouseButtonEventHandler(MouseLeftClickUp);
//            #endregion

//            double width = _node.Shape.Width;
//            double height = _node.Shape.Height;
//            _textbox.Width = width;
//            _textbox.Height = height;
//            RefreshPosition();
//        }

//        public UIElement[] ToUIElement()
//        {
//            List<UIElement> uiList = new List<UIElement>();
//            uiList.Add(this._textbox);
//            uiList.AddRange(_line.ToUIElement());
//            return uiList.ToArray<UIElement>();
//        }

//        #region Focus
//        /// <summary>
//        /// Set Focus
//        /// </summary>
//        public void Focus()
//        {
//            this._canvas.ClearFocus();
//            this._isFocus = true;

//            setLayout();

//            // set property box_isFocus
//            MainWindow mainWin = MainWindow.GetCurrentWindowsInstance(this._textbox);
//            if (mainWin == null)
//            {
//                return;
//            }
//            mainWin.SetPropertyInfo(this.FeatureInstance);
//        }
//        /// <summary>
//        /// Clear Focus
//        /// </summary>
//        public void ClearFocus()
//        {
//            _isFocus = false;
//            setLayout();
//        }
//        #endregion

//        #region Mouse Event Hanlder
//        /// <summary>
//        /// Mouse Move
//        /// </summary>
//        /// <param name="sender"></param>
//        /// <param name="args"></param>
//        public void MouseMove(object sender, MouseEventArgs args)
//        {
//            if (args.LeftButton != MouseButtonState.Pressed)
//            {
//                return;
//            }
//            if (_isPressed == false
//                || _orgPlace == null)
//            {
//                return;
//            }

//            Point curPt = args.MouseDevice.GetPosition(this._canvas);
//            double curMouseX, curMouseY;
//            curMouseX = curPt.X;
//            curMouseY = curPt.Y;

//            double orgMouseX, orgMouseY;
//            lock (_orgPlace)
//            {
//                orgMouseX = _orgPlace.X;
//                orgMouseY = _orgPlace.Y;

//                // reset org place
//                _orgPlace = new FMPoint(curMouseX, curMouseY);
//            }

//            double orgShapeX, orgShapeY;
//            orgShapeX = _node.Shape.Center.X;
//            orgShapeY = _node.Shape.Center.Y;

//            double curShapeX = curMouseX - orgMouseX + orgShapeX;
//            double curShapeY = curMouseY -orgMouseY + orgShapeY;

//            // set all children group relationship
//            FeatureTreeGroupRelationship[] grList = _node.ChildrenGroupRelationshipes;
//            if (grList == null || grList.Length == 0)
//            {
//                //curShapeX = curMouseX - orgMouseX + orgShapeX;
//                //curShapeY = curMouseY - orgMouseY + orgShapeY;
//                this.SetPosition(curShapeX, curShapeY, true);

//                return;
//            }

//            curShapeX = curMouseX - orgMouseX + orgShapeX;
//            curShapeY = curMouseY - orgMouseY + orgShapeY;
//            this.SetPosition(curShapeX, curShapeY, true);
//        }

//        /// <summary>
//        /// Mouse Left Click
//        /// </summary>
//        /// <param name="sender"></param>
//        /// <param name="args"></param>
//        public void MouseLeftClickDown(object sender, MouseButtonEventArgs args)
//        {
//            //// set cross tree constraint
//            //if (_panel.IsInCreatingCrossTreeConstraint)
//            //{
//            //    if (_panel.CrossTreeConstraintCurCount == 0)
//            //    {
//            //        _panel.FmCrossTreeNode1 = this.BindObject;
//            //        _panel.CrossTreeConstraintCurCount = 1;
//            //    }
//            //    else if (_panel.CrossTreeConstraintCurCount == 1)
//            //    {
//            //        if (_panel.FmCrossTreeNode1 != this.BindObject)
//            //        {
//            //            _panel.IsInCreatingCrossTreeConstraint = false;

//            //            AddNewCrossTreeConstraintWindow addwin =
//            //                new AddNewCrossTreeConstraintWindow(_panel
//            //                , _panel.FmCrossTreeNode1.ID, this.BindObject.ID);
//            //            addwin.ShowDialog();
//            //        }
//            //    }
//            //    else
//            //    {
//            //        throw new Exception();
//            //    }
//            //}

//            // set move information
//            Point pt = args.MouseDevice.GetPosition(this._canvas);
//            this._orgPlace = new FMPoint(pt.X, pt.Y);
//            this._isPressed = true;

//            // set focus
//            this.Focus();

//        }
//        /// <summary>
//        /// 
//        /// </summary>
//        /// <param name="sender"></param>
//        /// <param name="args"></param>
//        public void MouseLeftClickUp(object sender, MouseButtonEventArgs args)
//        {
//            _isPressed = false;
//        }
//        #endregion

//        #region Set Position
//        public void SetPosition(FMPoint point, bool toRefresh)
//        {
//            SetPosition(point.X, point.Y, toRefresh);
//        }

//        public void SetPosition(double x, double y, bool toRefresh)
//        {
//            double top = FeatureModelTree.MARGIN + FeatureModelTree.DEFAULT_HEIGHT / 2;
//            double left = FeatureModelTree.MARGIN + FeatureModelTree.DEFAULT_WIDTH / 2;
//            if (y < top)
//            {
//                y = top;
//            }
//            if (x < left)
//            {
//                x = left;
//            }

//            double orgX = _node.Shape.Center.X;
//            double orgY = _node.Shape.Center.Y;
//            FeatureTreeGroupRelationship[] grList = _node.ChildrenGroupRelationshipes;
//            if (grList != null || grList.Length != 0)
//            {
//                foreach (FeatureTreeGroupRelationship gr in grList)
//                {
//                    double grOrgX = gr.Shape.Center.X;
//                    double grOrgY = gr.Shape.Center.Y;

//                    double grCurX = x - orgX + grOrgX;
//                    double grCurY = y - orgY + grOrgY;
//                    double radius = gr.Shape.Radius;

//                    gr.Shape = new FMCircle(radius, new FMPoint(grCurX, grCurY));
//                }
//            }

//            FMPoint point = new FMPoint(x, y);
//            double width = _node.Shape.Width;
//            double height = _node.Shape.Height;
//            FMRectangle rect = new FMRectangle(width, height, point);

//            this._node.Shape = rect;
//            if (toRefresh) _canvas.RefreshPosition(this, null);
//            //_canvas.Refresh(this, null);
//        }
//        #endregion

//        private void setLayout()
//        {
//            #region Layout
//            _textbox.HorizontalContentAlignment = HorizontalAlignment.Center;
//            _textbox.VerticalContentAlignment = VerticalAlignment.Center;
//            _textbox.FontFamily = new FontFamily("Calibri");
//            _textbox.FontSize = Control_TreeRoot.DEFAULT_FONT_SIZE;
//            _textbox.BorderBrush = StrokeColor;
//            _textbox.Foreground = ForeColor;

//            _textbox.BorderThickness = new Thickness(BorderThickness, BorderThickness
//                , BorderThickness, BorderThickness);
//            _textbox.Background = this.Background;
//            #endregion
//        }

//        public void RefreshPosition()
//        {
//            double width = _node.Shape.Width;
//            double height = _node.Shape.Height;
//            double x = _node.Shape.Center.X;
//            double y = _node.Shape.Center.Y;
//            _textbox.Margin = new Thickness(x - width / 2, y - height / 2, 0, 0);

//            _line.RefreshPosition();
//        }
//    }

//}
